package com.cloudcc.mobile.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @Description: View 操作的工具类
 * @author wangzhiqiang
 * @date 2015-11-26 上午9:43:02
 * 
 */
public class ViewUtils {

	/**
	 * 给textview设置 数据 null 检测等
	 * 
	 * @param textview
	 * @param text
	 * @param defaultText
	 *            默认显示文字
	 */
	public static void setText(TextView textview, String text,
			String defaultText) {
		if (textview == null) {
			return;
		}
		if (TextUtils.isEmpty(text)) {
			textview.setText(defaultText);
		} else {
			textview.setText(text);
		}
	}

	/**
	 * textview设置 默认显示 ""
	 * 
	 * @param textview
	 * @param text
	 */
	public static void setText(TextView textview, String text) {
		setText(textview, text, "");
	}

	/**
	 * 获取listview的高度
	 * 
	 * @param listView
	 * @return
	 */
	public static int getTotalHeightofListView(ListView listView) {
		ListAdapter mAdapter = listView.getAdapter();
		if (mAdapter == null) {
			return 0;
		}
		int totalHeight = 0;
		for (int i = 0; i < mAdapter.getCount(); i++) {
			View mView = mAdapter.getView(i, null, listView);
			// mView.measure(
			// MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
			// MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			mView.measure(0, 0);
			totalHeight += mView.getMeasuredHeight();
		}
		totalHeight += (listView.getDividerHeight() * (mAdapter.getCount() - 1));
		return totalHeight;
	}

	/**
	 * 获取指定的 列数的高度
	 * 
	 * @param listView
	 * @param count
	 * @return
	 */
	public static int getHeightofListView(ListView listView, int count) {
		if (listView == null || count <= 0) {
			return 0;
		}
		ListAdapter mAdapter = listView.getAdapter();
		if (mAdapter == null) {
			return 0;
		}

		int totalHeight = 0;
		for (int i = 0; i < count; i++) {
			View mView = mAdapter.getView(i, null, listView);
			mView.measure(
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			// mView.measure(0, 0);
			totalHeight += mView.getMeasuredHeight();
		}
		totalHeight += (listView.getDividerHeight() * (count - 1));
		return totalHeight;
	}

	/**
	 * 获得测量高度
	 * 
	 * @param view
	 * @return
	 */
	public static int getViewMeasuredHeight(View view) {
		if (view == null) {
			return 0;
		}
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		return view.getMeasuredHeight();
	}

	public static int getViewMeasuredWidth(View view) {
		if (view == null) {
			return 0;
		}
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
				MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		return view.getMeasuredWidth();
	}

	/**
	 * 给 adpater以及子类设置emptyview
	 * 
	 * @param context
	 *            上下文
	 * @param adapterView
	 *            listview等
	 * @param emptyView
	 *            eptyview
	 */
	public static void addListViewEmptyView(Context context,
			AdapterView adapterView, View emptyView) {
		try {
			((ViewGroup) adapterView.getParent()).addView(emptyView);
			adapterView.setEmptyView(emptyView);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

}
